package com.example.orderproduct.service;

import com.example.orderproduct.bean.Order;
import com.example.orderproduct.dao.OrderDao;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @ClassName OrderService
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/7/6
 * @Version 1.0.0
 */
@Service
public class OrderService {

    //库存key
    private static final String KEY="ORDER-STOCK";
    //剩余库存
    private static final String RESIDUE_STOCK="RESIDUE-STOCK";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private OrderDao orderDao;

    public String createOrder(){

        Integer stock = (Integer) redisTemplate.opsForHash().get(KEY,RESIDUE_STOCK);
        System.out.println("剩余库存："+stock);
        String orderNo = UUID.randomUUID().toString();
        if(stock>0){
            rocketMQTemplate.convertAndSend("order-topic",orderNo);
        }else{
            return "秒杀结束！";
        }
        return "秒杀中……";
    }
}
